﻿using System.Collections.Generic;
using Hsi.Utility.Identity;
using IdentityAtRest.Utility.Identity;

namespace IdentityAtRest.Utility.Identity
{
    public class ExpressionAuthPolicy : IAuthorizationPolicy
    {
        private readonly string _permission;
        private readonly IExpression<bool> _policyExpression;
        private readonly string _explanation;

        public ExpressionAuthPolicy(string permission,IExpression<bool> policyExpression,string explanation)
        {
            _permission = permission;
            _policyExpression = policyExpression;
            _explanation = explanation;
        }

        public IAuthorization Evaluate(IList<IClaim> claims)
        {
            var context = new ExpressionContext(claims);
            context.Variables.AddResolutionService(new ClaimNameResolutionService(claims));
           
            return new DefaultAuthorization(_permission, _policyExpression.Evaluate(context), _explanation);
        }

    }
}